from urllib import request, parse
from lxml import etree
import ssl

# 取消 https 代理验证
ssl._create_default_https_context = ssl._create_unverified_context


def dylddetail(self, line, link):
    # 报头
    # user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36'
    user_agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
    headers = {'User-Agent': user_agent}
    url = link
    dyld_url = ''
    if line == '1':
        dyld_url = "http://dianyingleida.com"
    elif line == '2':
        dyld_url = "http://www.nmkan.com"

    req = request.Request(url, headers=headers)
    try:
        response = request.urlopen(req)
        # 获取每页的HTML源码字符串
        html = response.read().decode('utf-8')
        # 解析html 为 HTML 文档
        selector = etree.HTML(html)

        # 内容
        title = selector.xpath('//div[@class="myui-content__detail"]/h1[@class="title"]/text()')[0]
        poster = selector.xpath('//div[@class="myui-content__thumb"]/a/img/@data-original')[0]
        if poster.startswith('/upload'):
            poster = dyld_url + poster
        classfiy_title = selector.xpath('//div[@class="myui-content__detail"]/p[1]/a[1]/text()')[0]
        classfiy_area = selector.xpath('//div[@class="myui-content__detail"]/p[1]/a[2]/text()')[0]
        classfiy_year = selector.xpath('//div[@class="myui-content__detail"]/p[1]/a[3]/text()')[0]
        actor_list = selector.xpath('//div[@class="myui-content__detail"]/p[2]/a')
        actor_node_list = []
        for actor_node in actor_list:
            try:
                actor = actor_node.xpath('text()')[0]
                actor_node_list.append(actor)
            except Exception:
                actor = ""

        director = selector.xpath('//div[@class="myui-content__detail"]/p[3]/a/text()')[0]
        update_time = selector.xpath('//div[@class="myui-content__detail"]/p[4]/text()')[0]
        try:
            intro = selector.xpath('//div[@class="myui-content__detail"]/p[5]/span[@class="sketch"]/text()')[0]
        except Exception:
            intro = ""
        try:
            intro_all = selector.xpath('//div[@class="myui-content__detail"]/p[5]/span[@class="data"]/text()')[0]
        except Exception:
            intro_all = ""
        classfiy_link = dyld_url + selector.xpath('//div[@class="myui-content__detail"]/p[1]/a[1]/@href')[0]
        play_link = dyld_url + selector.xpath('//div[@class="operate clearfix"]/a[1]/@href')[0]
        content_list = {
            "title": title,
            "poster": poster,
            "classfiy_title": classfiy_title,
            "classfiy_link": classfiy_link,
            "classfiy_area": classfiy_area,
            "classfiy_year": classfiy_year,
            "play_link": play_link,
            "actor": actor_node_list,
            "director": director,
            "update_time": update_time,
            "intro": intro,
            "intro_all": intro_all,
        }

        # 播放源
        search_list = selector.xpath('//div[@class="myui-panel_hd"]//li/a')
        play_node_list = []
        for node in search_list:
            play_type = node.xpath('text()')[0]
            name = node.xpath('@href')[0][1:]
            name_list = selector.xpath('//div[@id="' + name + '"]//li')
            num_list = []
            for num in name_list:
                name_num = num.xpath('a/text()')[0]
                link_num = dyld_url + num.xpath('a/@href')[0]
                num_result = {
                    "name_num": name_num,
                    "link_num": link_num,
                }
                num_list.append(num_result)
            defalut_result = {
                "play_type": play_type,
                "list": num_list,
            }
            play_node_list.append(defalut_result)

        # 猜你喜欢
        like_list = selector.xpath('//ul[@class="myui-vodlist__bd clearfix"]/li/div')
        like_node_list = []
        for node in like_list:
            title = node.xpath('a/@title')[0]
            link = dyld_url + node.xpath('a/@href')[0]
            poster = node.xpath('a/@data-original')[0]
            if poster.startswith('/upload'):
                poster = dyld_url + poster
            definition = node.xpath('a/span[@class="pic-text text-right"]/text()')[0]
            intro = node.xpath('div[@class="myui-vodlist__detail"]/p/text()')[0]
            defalut_result = {
                "title": title,
                "link": link,
                "poster": poster,
                "definition": definition,
                "intro": intro,
            }
            like_node_list.append(defalut_result)

        # 最新更新
        update_list = selector.xpath('//ul[@class="myui-vodlist__text col-pd clearfix"]/li/a')
        update_node_list = []
        for node in update_list:
            title = node.xpath('@title')[0]
            link = dyld_url + node.xpath('@href')[0]
            definition = node.xpath('span[contains(@class,"text-muted")]/text()')[0]
            defalut_result = {
                "title": title,
                "link": link,
                "definition": definition,
            }
            update_node_list.append(defalut_result)

        result = {
            "content_list": content_list,
            "play_list": play_node_list,
            "like_list": like_node_list,
            "update_list": update_node_list,
        }

        return result
    except Exception:
        print(url, Exception)
        data = {"code": -1, "message": "error", "data": None}
        return data
